package com.work.formaldehyde.activity;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;
import com.wang.avi.AVLoadingIndicatorView;
import com.work.formaldehyde.R;
import com.work.formaldehyde.activityadapter.KePuListAdapter;
import com.work.formaldehyde.activityadapter.TuiHuoListAdapter;
import com.work.formaldehyde.model.KePuListModel;
import com.work.formaldehyde.model.TuiHuoModel;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TuiHuoListActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ImageView kepu_comeback;
    private TextView location_text;
    private AVLoadingIndicatorView back_loding_avi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApiUtils.ActivityIsLogin(this, 1);
        setContentView(R.layout.activity_tuihuo_list);
        kepu_comeback = findViewById(R.id.kepu_comeback);
        kepu_comeback.setOnClickListener(this);
        back_loding_avi = findViewById(R.id.back_loding_avi);
        if (ApiUtils.isNetworkConnected(TuiHuoListActivity.this))
            inti();
        else
            back_loding_avi.hide();
    }

    private ArrayList<TuiHuoModel.data> new_list_view;
    private ListView tuihuo_xq_list;
    private String type;
    private TuiHuoListAdapter myAdapter;

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    /**
     * 初始加载视图
     */
    public void Init() {
        Intent geti = getIntent();
        type = geti.getStringExtra("type");
        String name = geti.getStringExtra("name");
        location_text = findViewById(R.id.location_text);
        location_text.setText(name);
        tuihuo_xq_list = findViewById(R.id.tuihuo_xq_list);
        new_list_view = new ArrayList<>();

        if (!Url.USER_ID.equals("")) {
            okHttp_postFromParameters1();
        } else {
            back_loding_avi.hide();
            ApiUtils.SetToast(TuiHuoListActivity.this, "未登录");
        }
        tuihuo_xq_list.setOnItemClickListener(this);
    }

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        Init();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.kepu_comeback:
                onBackPressed();
                break;
        }
    }

    private void okHttp_postFromParameters1() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
//                    RequestBody formBody = new FormBody.Builder()
//                            .add("uid", Url.USER_ID).build();
//                    Request request = new Request.Builder().url(Url.GETTUIHUOLIST).post(formBody).build();
//                    okhttp3.Response response = okHttpClient.newCall(request).execute();
//                    String msg = response.body().string();
//                    Message message = new Message();
//                    message.obj = msg;
//                    gethot.sendMessage(message);
                    Request request = new Request.Builder().get().url(Url.GETTUIHUO + Url.USER_ID+"&logintoken="+Url.LOGIN_TOKEN).build();
                    Call call = okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                        }

                        @Override
                        public void onResponse(Call call, final Response response) {
                            try {
                                final String responseStr;
                                responseStr = response.body().string();
                                Message message = new Message();
                                message.obj = responseStr;
                                gethot.sendMessage(message);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).
                start();
    }

    public static TuiHuoModel jcbner;
    public Handler gethot = new Handler() {
        public void handleMessage(Message msg) {
            JSONObject success_Object;
            try {
                if (msg.obj != null) {
                    back_loding_avi.hide();
                    ApiUtils.SetLog("tuihuolist=" + msg.obj.toString());
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    if (code.equals("1")) {
                        Gson gson = new Gson();
                        jcbner = gson.fromJson(msg.obj.toString(),
                                TuiHuoModel.class);
                        new_list_view.addAll(jcbner.getData());
//                        myAdapter.notifyDataSetChanged();
                        myAdapter = new TuiHuoListAdapter(new_list_view,
                                TuiHuoListActivity.this, TuiHuoListActivity.this);
                        tuihuo_xq_list.setAdapter(myAdapter);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Intent tokpxq = new Intent(TuiHuoListActivity.this, KePuXQActivity.class);
//        tokpxq.putExtra("id", position + "");
//        tokpxq.putExtra("num", "1");
//        startActivity(tokpxq, ActivityOptions.makeSceneTransitionAnimation(TuiHuoListActivity.this).toBundle());
    }
}
